package haseman.project.Geonames;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

import org.geonames.Toponym;
import org.geonames.WebService;
import com.google.zxing.client.android.Main;
import com.google.zxing.client.android.R;

import android.graphics.Color;
import android.location.Location;
import android.util.Log;

public class GeonameClient {

	public static List<Toponym> places = new ArrayList<Toponym>();

	/**
	 * Method to return a vector of GeonameObjects that surronds my current
	 * location
	 * 
	 * @param loc
	 *            --> my current location by GPS
	 * @return
	 */
	public Vector<GeonameObject> getNearPlaces(Location loc,
			String[] featureCodes, double radius, int maxRows,String query) {

		Vector<GeonameObject> v = new Vector<GeonameObject>();
		places = new ArrayList<Toponym>();
		GeonameObject nearPlace;// = new GeonameObject(Main.ctx);
		try {
			Log.e("GeonameClient", "Radius is: " + radius);
			places = GeonamesWebService.findNearby(loc.getLatitude(), loc
					.getLongitude(), null, featureCodes, radius, maxRows);
		} catch (Exception e) {
			e.printStackTrace();

		}
		Log
				.e("GeonameClient", "***** places found " + places.size()
						+ " *****");
		//for (int i = 0; i < Math.min(Main.buttonPlaces.size(), places.size()); i++) {
		for (int i = 0; i < Math.min(5, places.size()); i++) {
			String featureCode = places.get(i).getFeatureCode();
			Log.e("GeonameClient", "Feature code: " + featureCode);
			String place = places.get(i).getName();
			if(!(featureCode.equals("TWO"))){
				Log.e("GeonameClient", "place name: " + place);
				Main.buttonPlaces.get(i).setText(place);
				Log.e("GeonameClient", "***** Place " + i + " " + place + " *****");
				nearPlace = new GeonameObject(Main.ctx);
				nearPlace.location = new Location(place);
				nearPlace.name = place;
				nearPlace.featureCode = featureCode;
				if (featureCode.equals("BLDG")) {

					Main.imageButtonPlaces.get(i)
							.setImageResource(R.drawable.build);
				} else if (featureCode.equals("HUT")) {

					Main.imageButtonPlaces.get(i).setImageResource(R.drawable.cafe);
				} else if (featureCode.equals("TWO")) {

					Main.imageButtonPlaces.get(i).setImageResource(
							R.drawable.office1);
				} else if (featureCode.equals("college")) {

					Main.imageButtonPlaces.get(i).setImageResource(
							R.drawable.classroom);
				} else if (featureCode.equals("AREA")) {

					Main.imageButtonPlaces.get(i).setImageResource(
							R.drawable.sports);
				} else if (featureCode.equals("PRK")) {

					Main.imageButtonPlaces.get(i).setImageResource(
							R.drawable.parking);
				}
				nearPlace.location.setLatitude(places.get(i).getLatitude());
				nearPlace.location.setLongitude(places.get(i).getLongitude());
				nearPlace.inclination = i * 2;

				v.add(nearPlace);
			}

			else{
			
			if(query.equals("no")){
				
			
				place= places.get(i).getName().substring(1);
				Log.e("GeoNameClient","place is "+place);
			}
			else if(((places.get(i).getName().charAt(0)+"").equals(query)))
			{
				place= places.get(i).getName().substring(1);
				Log.e("GeoNameClient","place is "+place);
			}
			else
				continue;
			Log.e("GeonameClient", "place name: " + place);
			Main.buttonPlaces.get(i).setText(place);
			Log.e("GeonameClient", "***** Place " + i + " " + place + " *****");
			nearPlace = new GeonameObject(Main.ctx);
			nearPlace.location = new Location(place);
			nearPlace.name = place;
			nearPlace.featureCode = featureCode;
			if (featureCode.equals("BLDG")) {

				Main.imageButtonPlaces.get(i)
						.setImageResource(R.drawable.build);
			} else if (featureCode.equals("HUT")) {

				Main.imageButtonPlaces.get(i).setImageResource(R.drawable.cafe);
			} else if (featureCode.equals("TWO")) {

				Main.imageButtonPlaces.get(i).setImageResource(
						R.drawable.office);
			} else if (featureCode.equals("college")) {

				Main.imageButtonPlaces.get(i).setImageResource(
						R.drawable.classroom);
			} else if (featureCode.equals("AREA")) {

				Main.imageButtonPlaces.get(i).setImageResource(
						R.drawable.sports);
			} else if (featureCode.equals("PRK")) {

				Main.imageButtonPlaces.get(i).setImageResource(
						R.drawable.parking);
			}
			nearPlace.location.setLatitude(places.get(i).getLatitude());
			nearPlace.location.setLongitude(places.get(i).getLongitude());
			nearPlace.inclination = i * 2;

			v.add(nearPlace);
		}
		
	}
		return v;
}
}